package com.knife.bbs.business;
/**
 * BBS权限管理类
 */
import java.util.Iterator;

import com.knife.bbs.object.BBSDir;
import com.knife.bbs.object.BBSDoc;
import com.knife.bbs.object.UserInfo;

public class BBSUserRole {
private boolean guest;
private boolean bbsManager=false;//属于论坛中的版主，该功能还没有实现
private boolean manager=false;//某一个版的版主
private boolean subManager=false;//某一个版的副版主
private boolean administrator=false;//超级管理员
private boolean author;//是否作者
private boolean validateUser;//是否合法用户

public static  BBSUserRole getUserRole(Object obj,ActiveUser user)
{	
	BBSUserService userService=new BBSUserService();
	BBSDirService dirService=new BBSDirService();
	BBSUserRole role=new BBSUserRole();
	role.setGuest(true);
	
	if(user==null ||(user.getUsername().equals(""))){	
		return role;	
	}
	UserInfo u=userService.getUserInfoByName(user.getUsername().trim());
	if(u==null){
		return role;
	}
	if(u.getStatus()==null || (u.getStatus().intValue()<0)){//用户已经被锁定
		return role;
	}	
	role.setValidateUser(true);
	for(Iterator it=BBSConfig.getInstance().getAdmins().iterator();it.hasNext();){
		String ad=(String)it.next();
		if(ad.equals(user.getUsername()))
		{			
			role.setAdministrator(true);
			break;
		}
	}
	
	if(obj instanceof BBSDir)
	{
		role.checkDirRole((BBSDir)obj,user);			
	}
	else if(obj instanceof BBSDoc)
	{
		BBSDoc doc=(BBSDoc)obj;		
		if(doc.getDirsn()!=null &&(! "".equals(doc.getDirsn()))){
		BBSDir dir=dirService.getBBSDirBySN(doc.getDirsn().trim());
		if(dir!=null)
		{
		 role.checkDirRole(dir,user);
		}
		if(doc.getInputuser()!=null && doc.getInputuser().trim().equals(user.getUsername().trim()))role.setAuthor(true);
		}
	}
	else if(obj instanceof UserInfo)
	{
		UserInfo u1=(UserInfo)obj;
		if(u1.getUsername()!=null){
		if(u1.getUsername().trim().equals(u.getUsername().trim()))role.setAuthor(true);
		}
	}
	return role;
}

public  void checkDirRole(BBSDir dir,ActiveUser user)
{
	if(dir.getSn()==null || "".equals(dir.getSn()))return;
	if(dir.getManager()!=null){
	if(dir.getManager().trim().equals(user.getUsername().trim()))
	{
		setManager(true);
	}}
	if(dir.getSubmanager()!=null){
	if(dir.getSubmanager().trim().equals(user.getUsername().trim()))
	{
		setSubManager(true);
	}	
	}
}

public boolean isBbsManager()
{
	return bbsManager;
}

public boolean isAdministrator() {
	return administrator;
}
public void setAdministrator(boolean administrator) {
	this.administrator = administrator;
}
public boolean isAuthor() {
	return author;
}
public void setAuthor(boolean author) {
	this.author = author;
}
public boolean isManager() {
	return manager;
}
public void setManager(boolean manager) {
	this.manager = manager;
}
public boolean isSubManager() {
	return subManager;
}
public void setSubManager(boolean subManager) {
	this.subManager = subManager;
}
public boolean isValidateUser() {
	return validateUser;
}
public void setValidateUser(boolean validateUser) {
	this.validateUser = validateUser;
}
public boolean isGuest() {
	return guest;
}
public void setGuest(boolean guest) {
	this.guest = guest;
}

}
